const { DataTypes } = require('sequelize')
const { sequelize } = require('../config/database')

const Chapter = sequelize.define('Chapter', {
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true
  },
  mangaId: {
    type: DataTypes.INTEGER,
    allowNull: false,
    references: {
      model: 'Mangas',
      key: 'id'
    }
  },
  title: {
    type: DataTypes.STRING,
    allowNull: false
  },
  chapterNumber: {
    type: DataTypes.FLOAT,
    allowNull: false
  },
  volume: {
    type: DataTypes.INTEGER,
    allowNull: true
  },
  pages: {
    type: DataTypes.JSON,
    defaultValue: []
  },
  pageCount: {
    type: DataTypes.INTEGER,
    defaultValue: 0
  },
  sourceUrl: {
    type: DataTypes.STRING,
    allowNull: false
  },
  sourceId: {
    type: DataTypes.STRING,
    allowNull: false
  },
  publishedAt: {
    type: DataTypes.DATE,
    allowNull: true
  },
  isActive: {
    type: DataTypes.BOOLEAN,
    defaultValue: true
  }
}, {
  indexes: [
    {
      fields: ['mangaId', 'chapterNumber']
    },
    {
      fields: ['mangaId', 'publishedAt']
    }
  ]
})

module.exports = Chapter
